import { Component, Input, OnInit } from '@angular/core';
import {
  FormGroup,
  FormBuilder,
  Validators
} from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';
import { Observable, of } from 'rxjs';

import { Role } from '../model/role';
import { PopupRes } from '@shared/popup/popup.interface';
import { I18NService } from '@core/i18n/i18n.service';
import { RoleService } from '../service/role.service';

import { comparisonValue } from '@shared/utils/data.utils';

@Component({
  selector: 'role-modify',
  templateUrl: './role-modify.html'
})

export class RoleModifyComponent implements OnInit, PopupRes {

  _id: string;

  @Input()
  set id(val: string) {
    this._id = val;
  }

  validateForm: FormGroup;

  searchOptions = [];

  tempItem: Role;

  isCheck = true;

  page = {
    pageNo: 1,
    pageSize: 999
  };

  constructor(
    private fb: FormBuilder,
    private message: NzMessageService,
    private i18nService: I18NService,
    private roleService: RoleService,
  ) {
    this.validateForm = this.fb.group({
      name: [null, [Validators.required]],
      rDesc: [null, [Validators.required]]
    });
  }

  ngOnInit(): void {
    if (this._id !== undefined) {
      this.getRole(this._id);
    }
  }

  submit(): Observable<any> {
    return new Observable((sub) => {
      if (this.isCheck) {
        this.isCheck = false;
        if (this._id !== undefined) {
          const saveVal: Role = {
            id: this._id,
            ...comparisonValue(this.validateForm.value, this.tempItem)
          };
          this.update(saveVal).subscribe(res => {
            sub.next();
          }, err => {
            sub.error();
          });
        } else {
          this.add(this.validateForm.value).subscribe(res => {
            sub.next();
          }, err => {
            sub.error();
          });
        }
      }
    });
  }

  cancel(): Observable<any> {
    return of('cancel');
  }

  getRole(id: string): void {
    this.roleService.getRoleById(id)
      .subscribe(res => {
        this.tempItem = res;
        this.validateForm.patchValue(res);
      });
  }

  add(item: Role): Observable<void> {
    return new Observable((sub) => {
      this.roleService.addRole(item)
        .subscribe(res => {
          if (res) {
            this.message.success(this.i18nService.translate('modify.return.add_success'));
            sub.next();
          } else {
            this.message.error(this.i18nService.translate('modify.return.add_error'));
            sub.error();
          }
          this.isCheck = true;
        }, err => {
          this.isCheck = true;
          sub.error();
        });
    });
  }

  update(item: Role): Observable<void> {
    return new Observable((sub) => {
      this.roleService.updateRole(item)
        .subscribe(response => {
          if (response) {
            this.message.success(this.i18nService.translate('modify.return.edit_success'));
            sub.next();
          } else {
            this.message.error(this.i18nService.translate('modify.return.eidt_error'));
            sub.error();
          }
          this.isCheck = true;
        }, err => {
          this.isCheck = true;
          sub.error();
        });
    });
  }

}
